package com.fruit.service.order.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fruit.constants.OrderConstant;
import com.fruit.constants.UserActionConstant;
import com.fruit.mapper.order.OrderMapper;
import com.fruit.pojo.order.Order;
import com.fruit.pojo.order.OrderGoods;
import com.fruit.pojo.user.MyFruitTree;
import com.fruit.pojo.user.UserAction;
import com.fruit.service.order.OrderManager;
import com.fruit.util.CustomUtil;
import com.fruit.util.DateUtil;

@Service("orderService")
public class OrderService implements OrderManager{
	
	@Autowired
	private OrderMapper orderMapper;
	
	@Value("${domain}")
	private String domain;
	
	@Override
	public int updateOrderState(long transactionId) {
		
		/*Order order = orderMapper.getOrderByTransactionId(transactionId);
		order.setState(OrderConstant.State.pay.getValue());
		int num = orderMapper.updateOrderStateById(order);
		if(num ==1){
			//查询商品列表
			List<OrderGoods> ogList = orderMapper.getOrderGoodsByOrderId(order.getId());
			for(OrderGoods og:ogList){
				String curTime = DateUtil.getTime();
				//修改商品库存
				orderMapper.updateGoodsSpecRealStockNum(og);
				//插入我的果树
				for(int i=0; i<og.getNumber(); i++){
					MyFruitTree mft = new MyFruitTree();
					mft.setUserId(og.getUserId());
					mft.setGoodsId(og.getGoodsId());
					mft.setGoodsSpecId(og.getGoodsSpecId());
					mft.setTitle(og.getTitle());
					mft.setThumb(og.getGoodsThumb());
					mft.setHarvestTime(og.getHarvestTime());
					mft.setFruitTreeState(og.getFruitTreeState());
					mft.setBuyTime(curTime);
					mft.setCreateTime(curTime);
					mft.setUpdateTime(curTime);
					orderMapper.saveMyFruitTree(mft);
					mft.setCode(CustomUtil.orderCode(3, mft.getId(), mft.getUserId()));
					num = orderMapper.updateMyFruitTreeCode(mft);
					if(num == 0){
						return 0;
					}
				}
				//插入用户动态
				UserAction ua = new UserAction();
				ua.setUserId(og.getUserId());
				ua.setAction(UserActionConstant.Action.buy.getValue()+og.getNumber()+og.getUnitDesc()+og.getTitle());
				ua.setCreateTime(curTime);
				ua.setType(UserActionConstant.Type.buy.getValue());
				ua.setObjId(og.getId());
				ua.setToUrl(domain+"/wap/goods/detail?id="+og.getGoodsId());
				orderMapper.saveUserAction(ua);
			}
			return 1;
		}*/
		
		return 0;
	}

	
}
